package com.qf.web.servlet;

import cn.dsna.util.images.ValidateCode;
import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;
import com.qf.utils.Base64Utils;
import com.qf.utils.StrUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/userservlet")
public class UserServlet extends BaseServlet{

    UserService userService = new UserServiceImpl();
    //退出功能
    //1清除session
    //2清除cookie
    public String logOut(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //1清除session
        request.getSession().removeAttribute("user");
        request.getSession().invalidate();
        //2清除cookie

        Cookie cookie = new Cookie("userinfo-2202", Base64Utils.encode(""));
        //生存时间
        cookie.setMaxAge(1);
        cookie.setPath("/");//在此网站任意路径可用
        response.addCookie(cookie);
        return "redirect:/login.jsp";

    }
    //登录
    public String login(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //判空
        if (StrUtils.isEmpty(username) || StrUtils.isEmpty(password)) {
            return "redirect:/login.jsp";
        }
        User user = userService.login(username,password);
        if (user!=null) {
            //把用户信息存储到session中
            request.getSession().setAttribute("user",user);
            String auto = request.getParameter("auto");
            if ("on".equalsIgnoreCase(auto)) {

                String userinfo = username+"#"+password;
                //把用户信息存储到cookie
                Cookie cookie = new Cookie("userinfo-2202", Base64Utils.encode(userinfo));
                //生存时间
                cookie.setMaxAge(14*24*60*60);
                cookie.setPath("/");//在此网站任意路径可用
                response.addCookie(cookie);
            }

            return "redirect:/index.jsp";
        }else{
            return "redirect:/login.jsp";
        }

    }


    //验证 验证码的正确与否
    public void checkCode(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //获取用户输入验证码，判断是否为空
        String code1 = request.getParameter("code");
        if (StrUtils.isEmpty(code1)) {
            response.getWriter().print("1");
        }
        String code2 = (String) request.getSession().getAttribute("code");
        if (code1.equalsIgnoreCase(code2)) {
            response.getWriter().print("0");

        }else {
            response.getWriter().print("1");
        }



    }

    //GUI:图形化
    public void code(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //创建验证码
        ValidateCode validateCode = new ValidateCode(100,40,3,10);
        //把验证码存到session
        request.getSession().setAttribute("code",validateCode.getCode());

        //输出验证码 到 响应流
        validateCode.write(response.getOutputStream());

    }


    //注册
    public String register(HttpServletRequest request, HttpServletResponse response) {
//        request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String gender = request.getParameter("gender");
        //判断username，email，password是否为空
        // 如果数据为null 跳转 register.jsp去重新注册
        if (StrUtils.isEmpty(username) || StrUtils.isEmpty(email) || StrUtils.isEmpty(password)) {
            return "redirect:/register.jsp";


        }
        //检测用户名是否存在
        boolean flag= userService.checkUserName(username);
        if (flag) {
            return  "redirect:/register.jsp";
        }
        //调用业务层 注册用户
        boolean isSuccess = userService.register(username,password,email,gender);
        if (isSuccess) {
            return  "redirect:/login.jsp";
        }else {
            return  "redirect:/register.jsp";
        }


    }

        public void checkUserName(HttpServletRequest request, HttpServletResponse response) throws IOException {
            //获取username
            String username = request.getParameter("username");
            //判断是否为空
            if (StrUtils.isEmpty(username)) {
                response.getWriter().print("0");
            }else{
                //发起校验（数据库）如果存在返回true，不存在返回false
                boolean flag= userService.checkUserName(username);
                if (flag) {
                    response.getWriter().print("1");
                }else{
                    response.getWriter().print("2");
                }
            }




        }
}
